//
//  AppInfo.m
//  barcodehair
//
//  Created by wai man lam on 19/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//



#import "DataInfo.h"
#import "SplashDataListItem.h"
#import "HomePageDataListItem.h"
#import "TabDataListItem.h"
#import "apps123_util.h"

@interface DataInfo()

@end

@implementation DataInfo

@synthesize requestedType;
@synthesize tomcatUrl;
@synthesize navBar;
@synthesize dataList;

#pragma mark - Private

- (NSMutableArray *)getAllTabTitles {
    [self getBarParams];
    [barParams.tabTitles removeAllObjects];
    
    for(int i = 0;i < [dataList count]; i++){
        if(i != 0){
            BaseItem *item = [dataList objectAtIndex:i];
            [barParams.tabTitles addObject:item.tabTitle];
        }
    }
    return barParams.tabTitles;
}

#pragma mark - Public

- (id)init{
    self = [super init];
    dataList = [[NSMutableArray alloc] init];
    
    return self;
}

- (id)copyWithZone:(NSZone *)zone {
    id copy = [[[self class] alloc] init];
    if (copy) {
        [copy setRequestedType:self.requestedType];
        [copy setTomcatUrl:self.tomcatUrl];
        [copy setDataList:self.dataList];
        
    }
    return copy;
}

/*
 Splash View
 */

- (BOOL)hasLoadingPage {
    [self getBarParams];
    
    if([dataList count] > 0){
        BaseItem *item = [dataList objectAtIndex:0];
        if([item isKindOfClass:[SplashDataListItem class]]){
            return YES;
        }
    }    
//#ifdef TEST_DEVELOPMENT_SANDBAY
//    return YES;
//#endif
    
    return NO;
}

- (NSString *)loadingPlaceHolder {
//#ifdef TEST_DEVELOPMENT_SANDBAY
//    return @"coupon0.png";
//#endif
    
    return @"";
}

- (NSString *)getLoadingPageCustomizeId {
    [self getBarParams];
    
#ifdef TEST_DEVELOPMENT_SANDBAY
    TabDataListItem *item = [dataList objectAtIndex:9];
    return item.customizeTabId;
#endif
    return @"";
}

/*
 NavigationBar and TabBar
 */

- (BarParams *)getBarParams {
    if(!barParams){
        barParams = [[BarParams alloc] init];
    
//        barParams.navigationBarheight = 44;
//        barParams.navigationBarheight = 50;
        barParams.tabBarButtonWidth = 320.0f / 5.0f;
        barParams.tabBarHeight = 44;
        barParams.contentViewYOffset = 20 + barParams.navigationBarheight;
        barParams.tabBarPaddingLeft = 0;
        barParams.tabBarPaddingTop = 0;
        barParams.tabBarGravity = 0;
        
        barParams.moreTitle = @"更多";
        barParams.moreNormalImage = @"icon-0061.png";
        barParams.moreHighlighImage = @"icon-0061c.png";
        
        HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
        NSMutableArray *customizeTabsList = homePageItem.customizeTabsList;
        
//        if([customizeTabsList count] > 5){
//            barParams.allowMore = YES;
//        }else{
//            barParams.allowMore = NO;
//        }
        if([homePageItem.hideFont isEqualToString:@"false"]){
            barParams.showTitle = YES;
        }else{
            barParams.showTitle = NO;
        }
        barParams.logo = [apps123_util abstractLastFix:homePageItem.logo];
        
        for(int i = 0;i < [customizeTabsList count]; i++){
            NSMutableArray *imageParams = [[NSMutableArray alloc] init];
            CustomizeTabsListItem *customItem = [customizeTabsList objectAtIndex:i];
            NSString *iconUrl = customItem.iconURL;
            iconUrl = [apps123_util abstractLastFix:iconUrl];
            
            [imageParams addObject:iconUrl];
            [imageParams addObject:[apps123_util append:iconUrl withLastFix:@"c"]];
            [barParams.barImageParams addObject:imageParams];
        }
        
        barParams.navigationBackground = self.navBar;

#ifdef TEST_DEVELOPMENT_SANDBAY

        barParams.navigationBarheight = 44;
        barParams.tabBarHeight = 61;
        barParams.tabBarPaddingLeft = 15;
        barParams.tabBarPaddingTop = 0;
        barParams.tabBarGravity = 1;
        barParams.contentViewYOffset = 20 + barParams.navigationBarheight;
        barParams.tabBarButtonWidth = ( 320.0f - barParams.tabBarPaddingLeft * 2 ) / 5.0f;
        barParams.allowMore = NO;
        
        barParams.background = @"";
        barParams.navigationBackground = @"nav-bg.png";
        barParams.menuBackground = @"category-bg.png";
        barParams.leftPaddingBackground = @"category-left.png";
        barParams.rightPaddingBackground = @"category-right.png";
        barParams.logo = @"logo.png";
        barParams.navigationBackground = @"nav-bg.png";
        
        barParams.maxTitleLines = 2;
        barParams.showTitle = YES;
        barParams.fontSize = 18;
        
        [barParams.barImageParams removeAllObjects];
        for(int i = 0;i < [customizeTabsList count]; i++){
            NSMutableArray *imageParams = [[NSMutableArray alloc] init];
            [imageParams addObject:@"category-list-bg.png"];
            [imageParams addObject:@"category-list-c.png"];
            [barParams.barImageParams addObject:imageParams];
        }
#else
        barParams.navigationBarheight = 44;
        barParams.tabBarButtonWidth = 320.0f / 5.0f;
        barParams.tabBarHeight = 0;
        barParams.contentViewYOffset = 20 + barParams.navigationBarheight;
        barParams.tabBarGravity = 0;
//        barParams.allowMore = YES;
#endif
        
    }
    
    return barParams;
}

- (NSString *)getNavigationBarBackground {
    return self.navBar;
}

- (NSString *)getNavigationBarLogo {
    HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
    return barParams.logo = [apps123_util abstractLastFix:homePageItem.logo];
}

/*
 Home Page
 */
- (NSInteger)getHomePageStyle {
    [self getBarParams];
    
    HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
    NSString *layout = homePageItem.layout;
    
#ifdef TEST_DEVELOPMENT_SANDBAY
    return 2;
#endif
    //1 means tab bar controller
    return 0;
}

- (NSString *)getHomePageStyleTemplate {
    //获取Layout
    [self getBarParams];
    
    HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
    
    //这里选择模板，并且返回对应的HomePage的Controller Class 类名
    
#ifdef TEST_DEVELOPMENT_SANDBAY
    return @"Home_PageLayout16TabBarController";
#endif
    
    NSString *tabId = homePageItem.customizeTabId;
    NSString *layout = homePageItem.layout;
    NSString *sysTabName = homePageItem.sysTabName;
    NSString *controller = [apps123_util generateControllerClassName:sysTabName layout:layout];
    
    return controller;
}

- (NSMutableArray *)getHomePageControllers {
    [self getBarParams];
    NSMutableArray *viewControllers = [NSMutableArray array];
    
    //获取DataList中的Home Page
    HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
//    NSMutableArray *tabItemsList = [dataList objectAtIndex:2];
    NSMutableArray *customizeTabsList = homePageItem.customizeTabsList;
    NSInteger viewControlellerCount = [customizeTabsList count];
    NSMutableArray *tabTitles = [self getAllTabTitles];

    if(barParams.allowMore){
        if(viewControlellerCount > 5)
            viewControlellerCount = 5;
    }
    for(int i = 0;i < viewControlellerCount; i++){
        if(barParams.allowMore && i == 4){
            [viewControllers addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"更多",@"Title",@"AppsMoreViewController",@"Controller",@"",@"TabId", nil]];
        }else{
            
            if(i == 0){
                NSString *tabId = homePageItem.customizeTabId;
                NSString *layout = homePageItem.layout;
                NSString *sysTabName = homePageItem.sysTabName;
                NSString *controller = [apps123_util generateControllerClassName:sysTabName layout:layout];
                
                [viewControllers addObject:[NSDictionary dictionaryWithObjectsAndKeys:[tabTitles objectAtIndex:i],@"Title",controller,@"Controller",tabId,@"TabId", nil]];
                
            }else{
                TabDataListItem *item = [dataList objectAtIndex:1 + i];
                NSString *tabId = item.customizeTabId;
                NSString *layout = item.layout;
                NSString *sysTabName = item.sysTabName;
                NSString *controller = [apps123_util generateControllerClassName:sysTabName layout:layout];
                
                [viewControllers addObject:[NSDictionary dictionaryWithObjectsAndKeys:[tabTitles objectAtIndex:i],@"Title",controller,@"Controller",tabId,@"TabId", nil]];
                
            }
        }
    }
    
    return viewControllers;
}

- (NSMutableArray *)getHomeControllers {
    [self getBarParams];
    NSMutableArray *viewControllers = [NSMutableArray array];
    
    //获取DataList中的Home Page
    HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
    NSMutableArray *customizeTabsList = homePageItem.customizeTabsList;
    NSInteger viewControlellerCount = [customizeTabsList count];
    
    if(barParams.allowMore){
        if(viewControlellerCount > 5)
            viewControlellerCount = 5;
    }
    for(int i = 0;i < viewControlellerCount; i++){
        if(barParams.allowMore && i == 4){
            [viewControllers addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"更多",@"Title",@"AppsMoreViewController",@"Controller",@"",@"TabId", nil]];
            
        }else{
            TabDataListItem *item = [dataList objectAtIndex:2 + i];
            NSString *tabId = item.customizeTabId;
            NSString *layout = item.layout;
            NSString *sysTabName = item.sysTabName;
            NSString *controller = [apps123_util generateControllerClassName:sysTabName layout:layout];
            [viewControllers addObject:[NSDictionary dictionaryWithObjectsAndKeys:item.tabTitle ? item.tabTitle : @"",@"Title",controller ? controller : @"",@"Controller",tabId ? tabId : @"",@"TabId", nil]];
            
        }
    }
    return viewControllers;
}

- (NSMutableArray *)getMoreControllers {
    NSMutableArray *viewControllers = [NSMutableArray array];
    
    //获取DataList中的Home Page
    HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
    NSMutableArray *customizeTabsList = homePageItem.customizeTabsList;
    NSInteger viewControlellerCount = [customizeTabsList count];
    NSMutableArray *tabTitles = [self getAllTabTitles];
    
    for(int i = 4;i < viewControlellerCount; i++){
        [viewControllers addObject:[NSDictionary dictionaryWithObjectsAndKeys:[tabTitles objectAtIndex:i],@"Title",@"HomePage1ViewController",@"Controller",@"",@"TabId", nil]];
    }
    
    return viewControllers;
}

- (NSString *)getHomePageBackground {
    //路径必须统一是 www/homepage/bg/xxxxx.png   沙湾是个特例,没有从bg下拿
    HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
    NSString *background = homePageItem.backgroundImg;
    background = [apps123_util abstractLastFix:background];
    return background;
}

- (NSString *)getHomePageMenuBackground {
    //路径必须统一是 www/homepage/menuBackground/xxxxx.png 
    HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
    NSString *background = homePageItem.menuBackground;
    background = [apps123_util abstractLastFix:background];
    return background;
}

- (NSMutableArray *)getHomePageMenuIcons {
    HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
    NSMutableArray *customizeTabsList = homePageItem.customizeTabsList;
    
    NSMutableArray *menuIcons = [NSMutableArray array];
    for(int i = 0;i < [customizeTabsList count]; i++){
        NSMutableArray *imageParams = [[NSMutableArray alloc] init];
        CustomizeTabsListItem *customItem = [customizeTabsList objectAtIndex:i];
        NSString *iconUrl = customItem.iconURL;
        iconUrl = [apps123_util abstractLastFix:iconUrl];
        
        [imageParams addObject:iconUrl];
        [imageParams addObject:[apps123_util append:iconUrl withLastFix:@"c"]];
        [menuIcons addObject:imageParams];
    }
    return menuIcons;
}

- (NSMutableArray *)getHomePageMenuTitles {
    HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
    NSMutableArray *customizeTabsList = homePageItem.customizeTabsList;
    
    NSMutableArray *menuTitles = [NSMutableArray array];
    for(int i = 0;i < [customizeTabsList count]; i++){
        CustomizeTabsListItem *customItem = [customizeTabsList objectAtIndex:i];
        NSString *tabTitle = customItem.tabTitle;
        [menuTitles addObject:tabTitle];
    }
    return menuTitles;
}

/*
 Tab
 */
- (NSString *)getTabBackground:(NSString *)tabId {
    NSString *background = @"";
    HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
    NSMutableArray *customizeTabsList = homePageItem.customizeTabsList;
    for(int i = 0;i < [customizeTabsList count]; i++){
        CustomizeTabsListItem *item = [customizeTabsList objectAtIndex:i];
        NSString *ID = item.ID;
        if([ID isEqualToString:tabId]){
            background = item.backGround;
            break;
        }
    }
    return background;
}

- (NSString *)getTabId:(NSInteger)sortIndex {
    if(sortIndex == 0){
        HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
        return homePageItem.customizeTabId;
    }else {
        TabDataListItem *tabItem = [dataList objectAtIndex:sortIndex + 1];
        return tabItem.customizeTabId;
    }
    return @"";
}

- (NSMutableDictionary *)getControllerInfo:(NSInteger)sortIndex {
    if(sortIndex == 0){
        HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
        
        NSString *tabId = homePageItem.customizeTabId;
        NSString *layout = homePageItem.layout;
        NSString *sysTabName = homePageItem.sysTabName;
        NSString *controller = [apps123_util generateControllerClassName:sysTabName layout:layout];
        NSString *tabTitle = homePageItem.tabTitle;
        
        return [NSDictionary dictionaryWithObjectsAndKeys:tabTitle ? tabTitle : @"",@"Title",controller ? controller : @"",@"Controller",tabId ? tabId : @"",@"TabId", nil];
        
    }else {
        TabDataListItem *tabItem = [dataList objectAtIndex:sortIndex + 1];
        
        NSString *tabId = tabItem.customizeTabId;
        NSString *layout = tabItem.layout;
        NSString *sysTabName = tabItem.sysTabName;
        NSString *controller = [apps123_util generateControllerClassName:sysTabName layout:layout];
        NSString *tabTitle = tabItem.tabTitle;
        
        return [NSDictionary dictionaryWithObjectsAndKeys:tabTitle ? tabTitle : @"",@"Title",controller ? controller : @"",@"Controller",tabId ? tabId : @"",@"TabId", nil];
    }
    return nil;
}

- (NSString *)getTabTitle:(NSInteger)sortIndex {
    if(sortIndex == 0){
        HomePageDataListItem *homePageItem = [dataList objectAtIndex:1];
        return homePageItem.tabTitle;
    }else {
        TabDataListItem *tabItem = [dataList objectAtIndex:sortIndex + 1];
        return tabItem.tabTitle;
    }
    return @"";
}

- (NSString *)getDetailLayout:(NSString *)tabId {
    for(int i = 0;i < [dataList count]; i++){
        if(i == 1){
            HomePageDataListItem *homeItem = [dataList objectAtIndex:i];
            if([homeItem.customizeTabId isEqualToString:tabId]){
                NSString *detailUrl = homeItem.detailUrl;
                if(detailUrl){
                    NSString *detailLayout = [apps123_util abstractLastFix:detailUrl];
                    if(detailLayout){
                        NSString *sysTabName = homeItem.sysTabName;
                        NSString *layout = homeItem.layout;
                        NSString *detailLayoutController = [apps123_util generateLayoutControllerClassName:sysTabName layout:layout detailLayout:detailLayout];
                        return detailLayoutController;
                    }
                }
            }
        }else if(i > 1){
            TabDataListItem *tabItem = [dataList objectAtIndex:i];
            if([tabItem.customizeTabId isEqualToString:tabId]){
                NSString *detailUrl = tabItem.detailUrl;
                if(detailUrl){
                    NSString *detailLayout = [apps123_util abstractLastFix:detailUrl];
                    if(detailLayout){
                        NSString *sysTabName = tabItem.sysTabName;
                        NSString *layout = tabItem.layout;
                        NSString *detailLayoutController = [apps123_util generateLayoutControllerClassName:sysTabName layout:layout detailLayout:detailLayout];
                        return detailLayoutController;
                    }
                }
            }
        }
    }
    return nil;
}

@end
